package com.gxuwz.endrskyweather.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.gxuwz.endrskyweather.R;
import com.gxuwz.endrskyweather.model.City;
import java.util.List;

public class FavoriteCityAdapter extends RecyclerView.Adapter<FavoriteCityAdapter.CityViewHolder> {
    private List<City> cities;

    public FavoriteCityAdapter(List<City> cities) {
        this.cities = cities;
    }

    @NonNull
    @Override
    public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_favorite_city, parent, false);
        return new CityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CityViewHolder holder, int position) {
        City city = cities.get(position);
        holder.tvCityName.setText(city.getAreaCn());
        holder.tvCityCode.setText("城市代码：" + city.getAreaCode());
        holder.tvProvince.setText("所属省份：" + city.getProvCn());
    }

    @Override
    public int getItemCount() {
        return cities != null ? cities.size() : 0;
    }

    public void updateData(List<City> newCities) {
        cities = newCities;
        notifyDataSetChanged();
    }

    static class CityViewHolder extends RecyclerView.ViewHolder {
        TextView tvCityName;
        TextView tvCityCode;
        TextView tvProvince;

        public CityViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCityName = itemView.findViewById(R.id.tv_city_name);
            tvCityCode = itemView.findViewById(R.id.tv_city_code);
            tvProvince = itemView.findViewById(R.id.tv_province);
        }
    }
}
